//index.js
//获取应用实例
const app = getApp();

Page({
  data: {
    collected:true,
    nickName: '123',
    avatarUrl: null,
    renwu:[{
      type:1,
    },{
      type:2
    }],
    TabCur: 0,
    wardrobe: [{
      name: '表情',
      type: 'cy',
      num: 8,
      imgShow: true,
      offset: 1,
      double: true,
      badge: false
    }, {
      name: '衣服',
      type: 'mj',
      num: 6,
      imgShow: true,
      offset: 1,
      double: true,
      badge: false
    }, {
      name: '裤子',
      type: 'xf',
      num: 5,
      imgShow: true,
      offset: 1,
      double: true,
      badge: false
    }, {
      name: '鞋子',
      type: 'xk',
      num: 5,
      imgShow: true,
      offset: 1,
      double: true,
      badge: false
    }],
    list: ['cy', 'mj', 'xf', 'xk']
  },
  //更改函数
  change_pic1(e){
    this.setData({
     collected:false
    })
    },
    change_pic2(e){
      this.setData({
       collected:true
      })
      },
    //保存
    gobaocun(param){
      wx.navigateTo({
        url: '/pages/index/baocun',
        })
    },
  //事件处理函数
  bingImgChange(e) {
    
    let type = e.currentTarget.dataset.type;
    let cur = e.currentTarget.dataset.cur;
    this.resetData(type, cur);
  },
  resetData(type, cur) {
    let flag = false;
    let fs = false;
    let badge = 0;
    let arr = this.data.list;
    let idx = 0;
    arr.forEach(key => {
      if (key == type) {
        let offset = this.data.wardrobe[idx].offset;
        let double = this.data.wardrobe[idx].double;
        //console.log("cur:"+cur+",offset:"+offset+",double:"+double);
        if ((offset == cur) && !double) {
          flag = true;
          fs = true;
        }
        if (!flag) {
          badge = true;
        }
        let changeOffset = "wardrobe[" + idx + "].offset";
        let changeImgShow = "wardrobe[" + idx + "].imgShow";
        let changeDouble = "wardrobe[" + idx + "].double";
        let changeBadge = "wardrobe[" + idx + "].badge";
        this.setData({
          [changeOffset]: cur,
          [changeImgShow]: flag,
          [changeDouble]: fs,
          [changeBadge]: badge
        })
      }
      idx++;
    });
  },
  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id - 1) * 60
    })
  },
  clearAll() {
    let that = this;
    wx.showModal({
      title: '重置',
      content: '是否要重新换装？',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          let arr = that.data.wardrobe;
          let idx = 0;
          arr.forEach(key => {
            let changeOffset = "wardrobe[" + idx + "].offset";
            let changeImgShow = "wardrobe[" + idx + "].imgShow";
            let changeDouble = "wardrobe[" + idx + "].double";
            let changeBadge = "wardrobe[" + idx + "].badge";
            that.setData({
              [changeOffset]: 1,
              [changeImgShow]: true,
              [changeDouble]: true,
              [changeBadge]: false
            })
            idx++;
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  onLoad: function() {
    console.log(app.globalData.userInfo);
    console.log(app.globalData.userInfo.nickName);
    console.log("onload");
    this.setData({
      nickName: app.globalData.userInfo.nickName,
      avatarUrl: app.globalData.userInfo.avatarUrl
    })
    // let nickName = null;
    // wx.getUserInfo({
    //   lang: 'zh_CN',
    //   success: function(res){
    //     var userInfo = res.userInfo,
    //     nickName = userInfo.nickName,
    //     var avatarUrl = userInfo.avatarUrl,
    //     var gender = userInfo.gender //性别 0：未知、1：男、2：女
    //     var province = userInfo.province
    //     var city = userInfo.city
    //     var country = userInfo.country
    //   }
    // })
    // console.log(nickName);
    // 查看是否授权
    // wx.getSetting({
    //   success (res){
    //     if (res.authSetting['scope.userInfo']) {
    //       // 已经授权，可以直接调用 getUserInfo 获取头像昵称
    //       wx.getUserInfo({
    //         success: function(res) {
    //           console.log(res.userInfo);
    //           this.setData({
    //             nickName: res.userInfo.nickName,
    //             avatarUrl: res.userInfo.avatarUrl,
    //           })
    //         }
    //       })
    //     }
    //   },
      //如果未授权，尝试拉取用户信息
  //     fail(res){
  //       wx.getSetting({
  //         success(res) {
  //           if (!res.authSetting['scope.userInfo']) {
  //             wx.authorize({
  //               scope: 'scope.userInfo',
  //               success () {
  //               wx.getUserInfo()
  //           }
  //         })
  //       }
  //     }
  //   })
  //     }
  //   })
  // },
  // getUserProfile(e) {
  //   // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
  //   wx.getUserProfile({
  //     desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
  //     success: (res) => {
  //       console.log(res)
  //       this.setData({
  //         userInfo: res.userInfo,
  //         hasUserInfo: true
  //       })
  //     }
  //   })
 }
})
